import { Component, Injector } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { UserService } from '../../providers/UserService';
import { BasePage } from '../basePage';
import { StorageService } from '../../providers/StorageService';
import { THROW_IF_NOT_FOUND } from '@angular/core/src/di/injector';


/**
 * Generated class for the FeedbackPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-feedback',
  templateUrl: 'feedback.html',
})
export class FeedbackPage extends BasePage {

  user = {
    user_id: '',
    feedback_content: '',
    contact: ''
  }
  userinfo:any;   //用户信息

  constructor(injector: Injector, public userService: UserService,public stc:StorageService) {
    super(injector)
    this.userinfo=this.stc.read("userinfo");
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad FeedbackPage');
    this.user.user_id = this.userinfo['user_id'];
  }



  async submit() {
    console.log(this.user);
    if (!(this.user.feedback_content == '')) {
      if(this.user.contact != ""){
        if ((/^1[34578]\d{9}$/.test(this.user.contact)) || (/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(this.user.contact)) || (/^[1-9]\d{4,9}$/.test(this.user.contact))) {
          let res = await this.userService.feedBackAPI(this.user);
          console.log(res)
          if (res.error_code == 0) {
            let toast = this.toastCtrl.create({
              message: res.msg,
              duration: 2000,
              position: "bottom"
            });
            toast.present();
          }
        } else {
          let toast = this.toastCtrl.create({
            message: '联系方式不正确',
            duration: 2000,
            position: "bottom"
          });
          toast.present();
        }
      }else{
        let toast = this.toastCtrl.create({
          message:'联系方式不能为空',
          duration: 2000,
          position: "bottom"
        });
        toast.present();
      }
    } else {
      let toast = this.toastCtrl.create({
        message:'反馈不能为空',
        duration: 2000,
        position: "bottom"
      });
      toast.present();
    }
  }

  back() {
    this.navCtrl.pop();
  }

}
